<?php
class UserController extends BootstrapController
{
	public function indexAction()
	{
		$auth = CF_Auth::getInstance();
		$auth->on('authenticate', array($this, 'verifyOnAuthenticate'));
		
		if($auth->has()) return;
		
		$email = 'tcl_java@yahoo.com';
		$password = '123456';
		
		$authAdapter = new CF_Auth_Adapter_Db();
		$authAdapter->getDbSelect()
			->table('##_users')
			->where(new CF_Db_Expr('`email`=%s AND password=MD5(%s)', array($email, $password)));
		
		$auth->authenticate($authAdapter);
		
		if($auth->has()){
			$this->getResponse()->getHeader()->redirect('http://localhost:10088/icafe/public/');
		}else{
			die('Login error');
		}
		
		$this->getView()->setRender(false);
	}
	
	/**
	 * 
	 * @param CF_Auth $auth
	 * @param object $result
	 */
	public function verifyOnAuthenticate($auth, $result)
	{
		if(!$result || $result->active < 1){
			$auth->getStorage()->clear();
			die('Account not ativated');
		}
	}
}